package com.redteam.data.dao;

import com.redteam.data.beans.Airplane;
import com.redteam.data.beans.MaintenanceEngineer;
import com.redteam.data.beans.MaintenanceMemo;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * DAO for retrieving <code>MaintenanceMemo</code> data
 * @author Jezreel Wilson
 * @author Steven Lawton
 * @version 1.0
 */
public class MaintenanceMemoDAO extends AbstractDAO implements DAO {

    private static MaintenanceMemoDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new MaintenanceMemoDAO();
    }

    private MaintenanceMemoDAO() {
        getDataSource();
        tableName = "HIGHFLIERS.MaintenanceRecords";
    }

    /**
     * Static method for receiving <code>MaintenanceMemoDAO</code>
     * @return
     * MaintenanceMemoDAO
     */
    public static MaintenanceMemoDAO getInstance() {
        return dao;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");
        } catch (NamingException ex) {
            Logger.getLogger(MaintenanceMemoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object obj) {
        MaintenanceMemo memo = (MaintenanceMemo) obj;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(getTableName()).append(" SET DISCREPANCY = ").append(memo.getDiscrepancy());
        sb.append(", PERSONS_ID = ").append(memo.getReporterId());
        sb.append(", AIRPLANES_ID = ").append(memo.getAirplane().getId());
        sb.append(", MECHANICS_ID = ").append(memo.getMechanic());
        sb.append(", MECHANIC_NOTES = '").append(memo.getNotes());
        sb.append("', STATUS = '").append(memo.getState());
        sb.append("', DATE_STARTED = '").append(memo.getDateMade());
        sb.append("', DATE_COMPLETED = '").append(memo.getDateComplete());
        sb.append("' WHERE ID = ").append(memo.getID());
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public MaintenanceMemo getObjectOut(ResultSet rs) {
        MaintenanceMemo memo = new MaintenanceMemo();
        try {
            DAO adao = AirplaneDAO.getInstance();
            memo.setAirplane((Airplane) adao.getSingle(rs.getLong("AIRPLANES_ID")));
            adao = MechanicDAO.getInstance();
            memo.setMechanic((MaintenanceEngineer) adao.getSingle(rs.getLong("MECHANICS_ID")));
            adao = PilotDAO.getInstance();
            memo.setReporterId(rs.getLong("PERSONS_ID"));
            memo.setID(rs.getLong("ID"));
            System.out.println( rs.getLong("ID"));
            memo.setDiscrepancy(rs.getString("DISCREPANCY"));
            memo.setNotes(rs.getString("MECHANIC_NOTES"));
            System.out.println( rs.getString("MECHANIC_NOTES"));
            memo.setState(rs.getString("STATUS"));
            System.out.println( rs.getString("STATUS"));
            memo.setDateMade(rs.getDate("DATE_STARTED"));
            memo.setDateComplete(rs.getDate("DATE_COMPLETED"));


        } catch (SQLException ex) {
            Logger.getLogger(MaintenanceMemoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return memo;
        }
    }

    /**
     * Gets all <code>MaintenanceMemos</code> for a specific <code>Mechanic</code>
     * @param maintenanceId
     * Id of <code>Mechanic</code> that you are specifying.
     * @return
     * List of MaintenanceMemos
     * @throws SQLException
     */
    public List<Object> getAllforMechanic(long maintenanceId) throws SQLException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        List<Object> objectList = new ArrayList<Object>();
        conn = this.getDS().getConnection();
        st = conn.createStatement();
        StringBuilder sb = new StringBuilder("SELECT * FROM ").append(this.getTableName());
        sb.append(" WHERE MECHANICS_ID= ").append(maintenanceId);
        rs = st.executeQuery(sb.toString());
        while (rs.next()) {
            objectList.add(this.getObjectOut(rs));
        }
        return objectList;
    }

    /**
     * Gets all <code>MaintenanceMemos</code> for a specific <code>Airplane</code>
     * @param airplaneId
     * Id of <code>Airplane</code> that you are specifying.
     * @return
     * List of MaintenanceMemos
     * @throws SQLException
     */
    public List<Object> getAllforPlane(long airplaneId) throws SQLException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        List<Object> objectList = new ArrayList<Object>();
        conn = this.getDS().getConnection();
        st = conn.createStatement();
        StringBuilder sb = new StringBuilder("SELECT * FROM ").append(this.getTableName()).append(" WHERE MECHANICS_ID= ").append(airplaneId);
        rs = st.executeQuery(sb.toString());
        while (rs.next()) {
            objectList.add(this.getObjectOut(rs));
        }
        return objectList;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object obj) {
        MaintenanceMemo memo = (MaintenanceMemo) obj;
        StringBuilder sb = new StringBuilder("INSERT INTO ").append(this.getTableName());
        sb.append(" (DISCREPANCY,PERSONS_ID,AIRPLANES_ID,MECHANICS_ID,MECHANIC_NOTES,STATUS,");
        sb.append("DATE_STARTED,DATE_COMPLETED)");
        sb.append(" VALUES ('").append(memo.getDiscrepancy()).append("',").append(memo.getReporterId()).append(",");
        sb.append(memo.getAirplane().getId()).append(",").append(memo.getMechanic().getId()).append(",'");
        sb.append(memo.getNotes()).append("','").append(memo.getState()).append("','");
        sb.append(memo.getDateMade()).append("','").append(memo.getDateComplete()).append("')");
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object o) {
        MaintenanceMemo memo = (MaintenanceMemo) o;
        return memo.getID();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return tableName;
    }
}
